package com.cskaoyan.service.wx.impl;

import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.po.MarketCollect;
import com.cskaoyan.bean.po.MarketCollectExample;
import com.cskaoyan.bean.po.MarketUser;
import com.cskaoyan.bean.vo.wx.WxCollectVo;
import com.cskaoyan.mapper.MarketCollectMapper;
import com.cskaoyan.service.wx.WxCollectService;
import com.cskaoyan.util.GetInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.util.Date;
import java.util.List;

/**
 * @author jzp
 * @since 2022/11/22 14:52
 */
@Service
public class WxCollectServiceImpl implements WxCollectService {
    @Autowired
    MarketCollectMapper marketCollectMapper;


    @Override
    public CommonData list(Integer type, Integer page, Integer limit) {

        MarketUser marketUser = GetInfo.getWxLoginInfo();
        Integer userId = marketUser.getId();

        PageHelper.startPage(page, limit);
        List<WxCollectVo> list = marketCollectMapper.selectList(type, userId);

        PageInfo<WxCollectVo> pageInfo = new PageInfo<>(list);


        return CommonData.data(pageInfo);
    }

    @Override
    public int addOrDelete(Integer type, Integer valueId) {
        MarketCollectExample example = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = example.createCriteria();
        criteria.andTypeEqualTo((byte) type.intValue());
        criteria.andValueIdEqualTo(valueId);
        criteria.andDeletedEqualTo(true);
        List<MarketCollect> marketCollects = marketCollectMapper.selectByExample(example);


        MarketUser marketUser = GetInfo.getWxLoginInfo();

        if (marketCollects.size() > 0) {
            // 说明有数据，然后取消收藏
            example.clear();
            MarketCollect collect = marketCollects.get(0);
            collect.setDeleted(false);
            Date date = new Date();
            collect.setUpdateTime(date);
            marketCollectMapper.updateByPrimaryKeySelective(collect);
            return 1;
        }
        example.clear();
        // 判断在表中是否已经存了相同的商品id收藏，如果有，则不插入表中
        MarketCollectExample.Criteria collectUserId = example.createCriteria();
        collectUserId.andUserIdEqualTo(marketUser.getId());
        collectUserId.andValueIdEqualTo(valueId);
        List<MarketCollect> collectExist = marketCollectMapper.selectByExample(example);
        if (collectExist.size() == 1){
            return 1;
        }

        example.clear();
        MarketCollect marketCollect = new MarketCollect();
        marketCollect.setType((byte) type.intValue());
        marketCollect.setValueId(valueId);
        marketCollect.setUserId(marketUser.getId());
        marketCollect.setDeleted(true);
        Date date = new Date();
        marketCollect.setAddTime(date);
        marketCollectMapper.insertSelective(marketCollect);
        return 1;
    }
}
